package com.tool.redis.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * @author 酒徒
 * @Version 1.0
 * @data 2024/1/22 17:00
 * 加入依赖，配置yml即可
 */
@Configuration
public class RedisConfiguration {

    /**
     * 非必需 Spring Boot 框架会自动装配 RedisTemplate 对象
     * 默认序列化器JdkSerializationRedisSerializer，存到redis中数据有差别，可以使用，不易观看
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        //设置redis工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //设置redis key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
